#include "ClearSky.h"
#include <cmath>

CClearSky::CClearSky()
{
//	m_paras.SetParameters(-1.0f, -0.32f, 10.0f, -3.0f, 0.45f, 1.0f);
//	m_zenithLuminance = 1.0f;
	m_images.clear();
}

void CClearSky::SetParas(CClearSkyParameter paras)
{
//	m_paras = paras;
}

void CClearSky::SetZenithLuminance(float zenithLuminance)
{
//	m_zenithLuminance = zenithLuminance;
}

float CClearSky::GetZenithLuminance()
{
	return 0.0f;
//	return m_zenithLuminance;
}

CSun* CClearSky::GetSun()
{
	return &m_sun;
}

//	deprecated
/*
float CClearSky::RelativeLuminance(float theta, float gamma)
{
	//	theta and gamma should be expressed in radians
	double cosG = cos(gamma);
	double cosT = cos(theta);
	double expFactor;
	if (abs(cosT) < EPS)
		expFactor = 0;
	else
		expFactor = exp(m_paras.m_b / cosT);
	return (float)((1 + m_paras.m_a * expFactor) 
		* (1 + m_paras.m_c * exp(m_paras.m_d * gamma) + m_paras.m_e * cosG * cosG));
}

float CClearSky::AbsoluteLuminance(float theta, float gamma)
{
	CSpherePosition solarPosition = m_sun.GetSolarPosition();
	return m_zenithLuminance * RelativeLuminance(theta, gamma) 
		/ RelativeLuminance(0, (float)M_PI / 2 - solarPosition.m_elevation);
}
*/
void CClearSky::AddNewSkyPhoto(CSkyPhoto photo)
{
	m_images.push_back(photo);
	//CalcExtrinsicParas();
}

void CClearSky::CalcSkyPhotoParas()
{
	int n = (int)m_images.size();
	for (int i = 0; i < n; i++)
	{
		m_images[i].CalcPixelSolarPosition();
		m_images[i].CalcExtrinsicParas();
	}
}

void CClearSky::InitSampleFromSkyPhoto(int index)
{
	m_images[index].InitSample();
}

void CClearSky::SampleFromSkyPhoto(int index, CSample sample)
{
	m_images[index].AddSample(sample.m_pixel, sample.m_color);
}

void CClearSky::SampleFromSkyPhoto(int index, int x, int y, int r, int g, int b)
{
	CPixel pixel;
	CColor color;
	pixel.m_x = x;
	pixel.m_y = y;
	color.m_r = r;
	color.m_g = g;
	color.m_b = b;
	m_images[index].AddSample(pixel, color);
}

CSkyPhoto CClearSky::GetSkyPhoto(int index)
{
	int n = m_images.size();
	index = (index >= 0) ? index : 0;
	index = (index < n) ? index : n - 1;
	return m_images[index];
}

void CClearSky::Glo3DPoint2SkyModelColor(float x, float y, float z, int &r, int &g, int &b)
{
	m_images[0].Glo3DPoint2SkyModelColor(x, y, z, r, g, b);
}

void CClearSky::SkyModelFitting(int index)
{
	m_images[index].SkyModelFitting();
}